#!/usr/bin/env python3
# -*- coding:utf-8 -*-
'''
@author: zhangshihao
@file: get_img_url.py
@time: 2022/5/10 21:10
'''
import json
from time import sleep

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'django_fptree_recommend.settings')  # mysite用的是自己的project
import django

django.setup()
from store.models import Product


def get_product_url(number, key_words):
    # 声明一个谷歌驱动器，并设置不加载图片，间接加快访问速度
    count = 0
    with open('static/stealth.min.js') as f:
        js = f.read()
    chrome_options = Options()
    chrome_options.add_experimental_option('prefs', {'profile.managed_default_content_settings.images': 2})
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
    chrome_options.add_argument("--disable-blink-features=AutomationControlled")
    # chrome_options.add_argument("headless")  # 不显示浏览器
    browser = webdriver.Chrome(options=chrome_options)
    browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": js
    })
    for key in key_words:
        img_list = []
        amazon_search = "https://www.amazon.com/s?k="
        product_url = amazon_search + str(key)
        browser.get(product_url)

        # with open('cookies.txt', 'r') as f:
        #     # 使用json读取cookies 注意读取的是文件 所以用load而不是loads
        #     cookies_list = json.load(f)
        #     for cookie in cookies_list:
        #         browser.add_cookie(cookie)

        try:
            lis = browser.find_elements(By.XPATH,
                                        '//a[@class="a-link-normal s-no-outline"]'
                                        '//div[@class="a-section aok-relative s-image-square-aspect"]//img')
            for li in lis:
                img_list.append(li.get_attribute('src'))
            if len(img_list) == 0:
                img_list.append("http://via.placeholder.com/200x250/FAEBD7")
        except Exception as e:
            img_list.append("http://via.placeholder.com/200x250/FAEBD7")
        count = count + 1

        save_to_model(key, img_list[0])
        print(count, key, img_list[0])


def save_to_model(p_name, url):
    products = Product.objects.filter(p_name=p_name)
    for product in products:
        product.p_url = url
        product.save()

if __name__ == "__main__":
    number = 1
    key_words = ["7 Deadly Zins"]
    get_product_url(number, key_words)